package com.wskeee.ec.components.tree
{
	import com.wskeee.ec.components.Image;
	import com.wskeee.ec.components.list.BaseListData;
	import com.wskeee.ec.components.list.ListDefaultRenderer;
	import com.wskeee.ec.containers.HBox;
	import com.wskeee.ec.containers.LayoutConst;
	import com.wskeee.ec.styles.Icons;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class TreeDefaultRenderer extends ListDefaultRenderer
	{
		//========================= init ========================
		override protected function initChildren():void
		{
			super.initChildren();
			contentBox = new HBox();
			contentBox.verticalAlign = LayoutConst.MIDDLE;
			contentBox.paddingLeft = 5;
			addChild(contentBox);
			reflashStyle()
			initSwitch();
			contentBox.addChild(icon);
			contentBox.addChild(label);
		}
		
		override public function reflashStyle():void
		{
			super.reflashStyle();
			openSwitchBMD = Icons.treeSwitchOpenIcon;
			closeSwitchBMD = Icons.treeSwitchCloseIcon;
			defaultOpenFolderBMD = Icons.treeFolderOpenIcon;
			defaultCloseFolderBMD = Icons.treeFolderCloseIcon;
			defaultLeafBMD = Icons.treeLeafIcon;
			switchChangedFlag = true;
			iconChangedFlag = true;
			
		}
		//========================= end =========================
		
		//========================= validate ====================
		override protected function commitProperties():void
		{
			super.commitProperties();
			if(!treeListData || !list)return;
			if(switchChangedFlag)
			{
				switchChangedFlag = false;
				switchIcon.source = treeListData.isOpen ? openSwitchBMD : closeSwitchBMD;
				switchIcon.visible = treeListData.hasChildren;
			}
			
			if(iconChangedFlag)
			{
				iconChangedFlag = false;
				icon.source = treeListData.hasChildren ? treeListData.isOpen ? defaultOpenFolderBMD : defaultCloseFolderBMD : defaultLeafBMD;
			}
		}
		
		override protected function updateDisplayList():void
		{
			if(!treeListData || !list)return;
			contentBox.validateNow();
			contentBox.y = (height - contentBox.height)/2;
			contentBox.x = (treeListData.depth - (Tree(list).showRoot ? 0 : 1)) * 10;
		}
		//========================= end =========================
		
		//========================= act =========================
		override public function set data(value:Object):void
		{
			super.data = value;
			if(listData)
			{
				switchChangedFlag = true;
				iconChangedFlag = true;
			}
			invalidateDisplayList();
		}
			
		override public function set listData(value:BaseListData):void
		{
			super.listData = value;
			switchChangedFlag = true;
			iconChangedFlag = true;
			invalidateDisplayList();
		}
		
		override public function destory():void
		{
			switchIcon.removeEventListener(MouseEvent.CLICK,onSwitchClick);
			super.destory();
		}
		//========================= end =========================
		
		//========================= ui ==========================
		//---------------------
		//layout
		//---------------------
		protected var contentBox:HBox;
		//---------------------
		//switch
		//---------------------
		protected var openSwitchBMD:Object;
		protected var closeSwitchBMD:Object;
		//开始容器
		protected var switchContainer:Sprite;
		//开关
		protected var switchIcon:Image;
		protected var switchChangedFlag:Boolean = false;
		protected function initSwitch():void
		{
			switchIcon = new Image();
			switchIcon.width = 16;
			switchIcon.height = 16;
			switchIcon.addEventListener(MouseEvent.CLICK,onSwitchClick);
			contentBox.addChild(switchIcon);
		}
		
		protected function onSwitchClick(evt:MouseEvent):void
		{
			if(evt)evt.stopPropagation();
			if(!treeListData.hasChildren)return;
			TreeListData(listData).isOpen = !TreeListData(listData).isOpen;
			var event:TreeEvent = new TreeEvent(TreeEvent.ITEM_OPEN,
				{
					isOpen:TreeListData(listData).isOpen,
					listData:listData
				});
			dispatchEvent(event);
		}
		//---------------------
		//icon
		//---------------------
		protected var defaultOpenFolderBMD:Object;
		protected var defaultCloseFolderBMD:Object;
		protected var defaultLeafBMD:Object;
		
		protected function get treeListData():TreeListData
		{
			return listData as TreeListData;
		}
		//========================= end =========================
	}
}